<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class PagesController extends AppController {
	public $name = 'Pages';
	public $uses = array('Page');

	public $title;
	
	function beforeRender() {
	    parent::beforeRender();
	    $result = $this->Page->getNewsItems(2);
	    
	    $length = 50;	    
	    $newsItems = array();
	    $i = 0;
	    $ellipsis = "...";
	    
	    foreach ( $result as $res ) {
	        $string = null;
 	        $words = explode(' ', $res['content']);
 	        if (count($words) > $length) {
 	            $string = implode(' ', array_slice($words, 0, $length)) . $ellipsis;
 	        }
 	        else {
 	            $string = $res['content'];
 	        }
 	        
 	        $item['title']   = $res['title'];
 	        $item['date']    = $res['date'];
 	        $item['key']     = $res['key'];
 	        $item['content'] = $string;
 	        $newsItems[$i] = $item;
 	        
 	        $i++;
	    }
 	    $this->set('newsItems', $newsItems);	     
	}

	public function display() {
		$path = func_get_args();

		$count = count($path);
		if (!$count) {
			$this->redirect('/');
		}
		$page = $subpage = $title_for_layout = null;

		if (!empty($path[0])) {
			$page = $path[0];
		}
		if (!empty($path[1])) {
			$subpage = $path[1];
		}
		if (!empty($path[$count - 1])) {
			$title_for_layout = Inflector::humanize($path[$count - 1]);
		}
		$this->set(compact('page', 'subpage', 'title_for_layout'));
		$this->render(implode('/', $path));
	}
	
	public function home() {
	    $this->set('content', $this->Page->getContent('home') );
	}
	
	public function diensten() {
	    $path      = $this->request->url;
	    $splitpath = split( '/', $path );
	    $page      = 'diensten';
	    
	    if ( count( $splitpath ) > 3 && $splitpath[3] != '' ) {
	        $page = $splitpath[3];
	        $this->set('content', $this->Page->getDienst($page) );
	    }
	    else if ( count( $splitpath ) > 2 && $splitpath[2] != '' ) {
	        $page = $splitpath[2];
	        $this->set('content', $this->Page->getDienst($page) );
	    }
	    else {
	        $this->set('content', $this->Page->getContent($page) );
	    }
	}
	
	public function about() {
	    $this->set('content', $this->Page->getContent('about') );
	}
	
	public function werk() {
	    $this->set('content', $this->Page->getContent('werk') );
	}
	
	public function referenties() {
	    $this->set('content', $this->Page->getContent('referenties') );
	}

	public function contact() {
	    $this->set('content', $this->Page->getContent('contact') );
	}
	
	public function nieuws() {
	    $this->set('nieuwsItems', $this->Page->getNewsItems(0));
	}
	
	public function nieuwsItem($item) {
	    $this->set('nieuwsItem', $this->Page->getNewsItem($item));
	}
	
}
